/**
 *	流程模块接口
 *	开发者：雨中磐石
 *	地址：www.rockoa.com
 *	日期：2023-07-19
 */

package com.framework.Module.REIM.imgroup;






import com.framerock.db.DB;
import com.framerock.baselib.Jiami;
import com.framerock.baselib.Json;
import com.framerock.baselib.Rock;

import com.framework.ACore.Model;
import com.framework.Flow.FlowListener;
import com.framework.Model.ModelCompany;
import com.framework.Model.ModelReim;

import java.util.ArrayList;
import java.util.Map;


public class FlowListenerimgroup extends FlowListener {



    @Override
    public Boolean onModejs() {
        return true;
    }

    @Override
    public Boolean onRemind() {
        return false;
    }

    @Override
    public void onDeleteafter() {
        Model.get(this.nDb, "imgroupuser").delete("`gid`="+mFlow.mid+"");
        //Model.get(this.nDb, "imhistory").delete("`type`=1 AND `sendid`="+mFlow.mid+"");
    }

    @Override
    public String onAction(String act, String params) {
        if(act.equals("savegroupuser"))return this.savegroupuser();
        if(act.equals("groupedit"))return this.groupedit();
        if(act.equals("chatinfo"))return this.chatinfo();
        return super.onAction(act, params);
    }

    //组下添加人员
    private String savegroupuser() {
        int gid     = mRequest.postInt("gid");
        int type    = mRequest.postInt("type");
        String sid  = mRequest.post("sid");
        ModelReim obj = ModelReim.get(this.nDb);
        obj.saveGroupUser(type, gid, sid, mRequest.COMPANYID);
        return Json.getJsonString(obj.groupInfo(gid));
    }

    //组信息和下的人员
    private String chatinfo(){
        Json json = new Json();
        int id   = mRequest.getInt("id");
        int type = mRequest.getInt("type");
        int ismanage = 0;
        ModelReim obj = ModelReim.get(this.nDb);
        json.putVal("info", obj.groupInfo(id));
        ArrayList<Map<String, String>> userarr = null;

        if(type==1){
            if(mRequest.USEATYPE>0)ismanage = 1;
            String sqwhe = DB.dbSql(DB.chuliPerfix("imgroupuser"), "sid", "`type`="+type+" AND `gid`="+id+"");
            userarr     = ModelCompany.get(this.nDb).companyUsera(mRequest.COMPANYID, "a.`id` IN("+sqwhe+")");
            if(ismanage==0)ismanage    = mFlow.mModel.getCount("`id`="+id+" AND `aid`="+mRequest.USEAID+"");
        }
        json.putVal("userarr", userarr);
        json.putInt("ismanage",ismanage);

        return json.getJson();
    }

    //修改群信息
    private String groupedit()
    {
        String id       = mRequest.get("id");
        String fields   = mRequest.get("fields");
        String arr[]    = "name,bqname,bqcolor,face,explain".split(",");
        if(Rock.inArray(arr, fields)) {
            String value = Jiami.base64decode(mRequest.get("value"));
            mFlow.mModel.update("`" + fields + "`='" + value + "'", id);
        }
        return "";
    }
}
